package com.timguan.render.dao.manager.cms;

import com.timguan.render.dao.domain.cms.CmsModuleDO;
import com.timguan.render.dao.domain.cms.CmsModuleDOExample;
import com.timguan.render.dao.domain.cms.CmsModuleDOWithBLOBs;
import com.timguan.render.dao.persistence.cms.CmsModuleDOMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by guankaiqiang on 2015/3/19.
 */
public class CmsModuleManagerImpl {
    @Resource
    private CmsModuleDOMapper cmsModuleDOMapper;

    public int insert(CmsModuleDOWithBLOBs record) {
        int id = 0;
        if (record == null) {
            return id;
        }
        List<CmsModuleDO> cmsModuleDOs = null;
        if (record.getModuleName() != null && record.getModuleName().length() != 0) {
            CmsModuleDOExample condition = new CmsModuleDOExample();
            condition.createCriteria().andModuleNameEqualTo(record.getModuleName()).andIsDeleteEqualTo(0);
            cmsModuleDOs = cmsModuleDOMapper.selectByExample(condition);
        }
        if (cmsModuleDOs == null || cmsModuleDOs.size() == 0) {
            Date now = new Date();
            record.setGmtCreate(now);
            record.setGmtModified(now);
            record.setIsDelete(0);
            cmsModuleDOMapper.insert(record);
            return record != null ? record.getId() : 0;
        } else {
            throw new RuntimeException("modulename " + record.getModuleName() + " exist!");
        }
    }

    public int countByExample(CmsModuleDOExample condition) {
        int count = 0;
        if (condition == null) {
            return count;
        }
        count = cmsModuleDOMapper.countByExample(condition);
        return count;
    }

    public List<CmsModuleDO> selectByExample(CmsModuleDOExample condition) {
        List<CmsModuleDO> cmsModuleDOs = null;
        if (condition == null) {
            return cmsModuleDOs;
        }
        cmsModuleDOs = cmsModuleDOMapper.selectByExample(condition);
        return cmsModuleDOs;
    }

    public List<CmsModuleDOWithBLOBs> selectByExampleWithBLOBs(CmsModuleDOExample condition) {
        List<CmsModuleDOWithBLOBs> cmsPageDOs = null;
        if (condition == null) {
            return cmsPageDOs;
        }
        cmsPageDOs = cmsModuleDOMapper.selectByExampleWithBLOBs(condition);
        return cmsPageDOs;
    }

    public CmsModuleDOWithBLOBs selectByModuleName(String moduleName) {
        CmsModuleDOExample condition = new CmsModuleDOExample();
//        condition.createCriteria().andModuleNameEqualTo(moduleName).andIsDeleteEqualTo(0);//对于废弃的module可以查询
        condition.createCriteria().andModuleNameEqualTo(moduleName);
        List<CmsModuleDOWithBLOBs> cmsModuleDOWithBLOBses = cmsModuleDOMapper.selectByExampleWithBLOBs(condition);
        if (cmsModuleDOWithBLOBses != null && cmsModuleDOWithBLOBses.size() != 0) {
            return cmsModuleDOWithBLOBses.get(0);
        }
        return null;
    }

    public CmsModuleDOWithBLOBs selectByPrimaryKey(int moduleId) {
        return cmsModuleDOMapper.selectByPrimaryKey(moduleId);
    }

    public int updateByPrimaryKeySelective(CmsModuleDOWithBLOBs record) {
        int count = 0;
        if (record == null) {
            return count;
        }
        Date now = new Date();
        record.setGmtModified(now);
        count = cmsModuleDOMapper.updateByPrimaryKeySelective(record);
        return count;
    }
}
